package com.wuzhishan.news.service.Impl;

import com.wuzhishan.news.mapper.*;
import com.wuzhishan.news.pojo.*;
import com.wuzhishan.news.service.homeService;
import com.wuzhishan.news.utils.numberTransUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
public class homeServiceImp implements homeService {
    @Autowired
    private followMapper followMapper;
    @Autowired
    private articleMapper articleMapper;
    @Autowired
    private favorMapper favorMapper;
    @Autowired
    private commentMapper commentMapper;
    @Autowired
    private notifyMapper notifyMapper;
    //分页显示属性
    PageBean pageBean = null;

    /**
     * 我的关注板块
     * @param userId
     * @param page
     * @param mv
     * @return
     */
    @Override
    public ModelAndView followers(long userId, String page, ModelAndView mv) {
        //设置当前页面属性
        int currentPage = Integer.parseInt(page);   //当前页号
        int pageSize = 5;                           //默认页面大小设置为5
        int pageIndex = (currentPage - 1)*pageSize; //页面索引
        List<FollowsVo> follow = new ArrayList<>();
        int articleCount = 0;
        //关注用户列表
        List<BlogFollows> follower = followMapper.selectFollower(userId,pageIndex,pageSize);
        for (BlogFollows blogFollows : follower) {
            FollowsVo f = new FollowsVo();
            BeanUtils.copyProperties(blogFollows,f);
            System.out.println(blogFollows);
            //设置关注者发布文章总数
            articleCount = articleMapper.countArticleOfUser((int) blogFollows.getTarget().getId());
            f.setArticleCount(articleCount);
            //设置关注者粉丝数
            int fansCount = followMapper.fansCount((int) blogFollows.getTarget().getId());
            f.setFansCount(fansCount);
            follow.add(f);
        }
        //用于分页功能参数设置
        pageBean = new PageBean();
        pageBean.setCurrentPage(currentPage);
        int totalCount = followMapper.followCount(userId);  //关注用户的总数量
        pageBean.setTotalPage(totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1);
        pageBean.setTotalCount(totalCount);
        mv.addObject("pageBean",pageBean);  //分页数据返回给界面
        mv.addObject("follow",follow);      //关注列表数据返回给用户
        return mv;
    }

    /**
     * 我的粉丝板块
     * @param userId
     * @param page
     * @param mv
     * @return
     */
    @Override
    public ModelAndView fans(long userId, String page, ModelAndView mv) {
        //设置当前页面属性
        int currentPage = Integer.parseInt(page);   //当前页号
        int pageSize = 5;                           //默认页面大小设置为5
        int pageIndex = (currentPage - 1)*pageSize; //页面索引
        List<FollowsVo> fans = new ArrayList<>();
        int articleCount = 0;
        //粉丝用户列表
        List<BlogFollows> follower = followMapper.selectFans(userId,pageIndex,pageSize);
        for (BlogFollows blogFollows : follower) {
            FollowsVo f = new FollowsVo();
            BeanUtils.copyProperties(blogFollows,f);
            //设置粉丝者发布文章总数
            articleCount = articleMapper.countArticleOfUser((int) blogFollows.getSource().getId());
            f.setArticleCount(articleCount);
            //设置粉丝的粉丝数
            int fansCount = followMapper.fansCount((int) blogFollows.getSource().getId());
            f.setFansCount(fansCount);
            fans.add(f);
        }
        pageBean = new PageBean();
        pageBean.setCurrentPage(currentPage);
        int totalCount = followMapper.followCount(userId);  //关注用户的总数量
        pageBean.setTotalPage(totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1);
        pageBean.setTotalCount(totalCount);
        mv.addObject("pageBean",pageBean);  //分页数据返回给界面
        mv.addObject("fans",fans);      //关注列表数据返回给用户
        return mv;
    }

    /**
     * 我的喜欢板块
     * @param userId
     * @param page
     * @param mv
     * @return
     */
    @Override
    public ModelAndView favor(long userId, String page, ModelAndView mv) {
        //设置当前页面属性
        int currentPage = Integer.parseInt(page);   //当前页号
        int pageSize = 5;                           //默认页面大小设置为5
        int pageIndex = (currentPage - 1)*pageSize; //页面索引
        //我的喜欢文章列表
        List<BlogFavor> favors = favorMapper.getFavorByUserId(userId);
        for (BlogFavor favor : favors) {
            ArticleVo v = new ArticleVo();
            BeanUtils.copyProperties(favor.getArticle(),v);
            //设置显示内容的长度
            System.out.println(v.getId()+"---------------");
//            v.setContent(v.getContent().substring(100).trim());
            //根据文章Id获取文章喜欢数目
            int likes = favorMapper.articleFavors(v.getId());
            v.setLikes(likes);
            //根据文章Id获取文章评论数
            int comments = commentMapper.countsOfArticle(v.getId());
            v.setComments(comments);
            favor.setArticle(v);
        }
        //设置分页显示数据
        pageBean = new PageBean();
        pageBean.setCurrentPage(currentPage);
        int totalCount = favorMapper.favorCount(userId);  //用户喜欢文章的总数量
        pageBean.setTotalPage(totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1);
        pageBean.setTotalCount(totalCount);
        mv.addObject("favors",favors);
        mv.addObject("pageBean",pageBean);
        return mv;
    }

    /**
     * 取消喜欢文章
     * @param articleId
     * @param userId
     * @return
     */
    @Override
    public Result unfavor(String articleId, long userId) {
        try {
            favorMapper.unfavor(articleId,userId);
            return Result.of(1,"取消成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.of(0,"服务器错误，请稍后重试!");
        }
    }

    /**
     * 获取用户通知，然后将所有通知标记为已读状态
     * @param target_id
     * @param page
     * @param mv
     * @return
     */
    @Override
    public ModelAndView notifies(long target_id, String page, ModelAndView mv) {
        //设置当前页面属性
        int currentPage = Integer.parseInt(page);   //当前页号
        int pageSize = 5;                           //默认页面大小设置为5
        int pageIndex = (currentPage - 1)*pageSize; //页面索引
        //获取用户所有通知
        List<BlogNotify> notifies = notifyMapper.getNotifies(target_id);
        //将该用户所有通知标记为已读状态
        notifyMapper.clearNotifies(target_id);
        //设置分页显示数据
        pageBean = new PageBean();
        pageBean.setCurrentPage(currentPage);
        int totalCount = notifyMapper.notifiesCount(target_id);  //用户未读通知的总数
        pageBean.setTotalPage(totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1);
        pageBean.setTotalCount(totalCount);
        mv.addObject("notifies",notifies);
        mv.addObject("pageBean",pageBean);
        mv.addObject("totalCount",totalCount);
        return mv;
    }

    /**
     * 获取当前用户通知
     * @param userId
     * @param mv
     * @return
     */
    @Override
    public ModelAndView notifiesCount(long userId, HttpSession session,ModelAndView mv) {
        int totalCount = notifyMapper.notifiesCount(userId);
        System.out.println("totalCount--------------"+totalCount);
        session.setAttribute("totalCount",totalCount);
        return mv;
    }
}
